# -*- coding:utf-8 -*-

from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.preprocessing import StandardScaler, MinMaxScaler
from sklearn.linear_model import LinearRegression,SGDRegressor,Ridge
from sklearn.metrics import mean_squared_error
import matplotlib.pyplot as plt
import joblib
import sys
import numpy as np

sys.path.append("../")
from frameworks.utils.PadasExcelUtil import *
import warnings
warnings.filterwarnings('ignore')

def main():
    data = pd.read_csv("H:/model/score.txt", encoding="utf-8", sep='\t')
    df = data.dropna()
    print("特征数量：\n", df.shape)

    df.query("codename=='金财互联'",inplace=True)
    print("特征数量：\n", df.shape)

    newdf = df[['flow_money',"score","money_score","zf"]]
    print(newdf)

    # 2）划分数据集
    X_train, X_test, y_train, y_test = train_test_split(newdf, df["rs_zf"], test_size=0.2, random_state=22)
    print(X_test)
    scaler = StandardScaler()
    X_train_scaled = scaler.fit_transform(X_train)
    X_test_scaled = scaler.transform(X_test)

    # 3. 使用GridSearchCV来优化alpha值
    # 定义alpha值的候选范围
    alpha_candidates = [1e-15, 1e-10, 1e-5, 1e-2, 1, 5, 10, 20]

    # 创建岭回归模型
    ridge = Ridge()

    # 创建GridSearchCV对象
    grid_search = GridSearchCV(estimator=ridge, param_grid={'alpha': alpha_candidates}, cv=5,
                               scoring='neg_mean_squared_error')

    # 执行网格搜索
    grid_search.fit(X_train_scaled, y_train)

    # 获取最佳alpha值
    best_alpha = grid_search.best_params_['alpha']
    print(f"Best alpha: {best_alpha}")

    # 使用最佳alpha值训练模型
    ridge_best = Ridge(alpha=best_alpha)
    ridge_best.fit(X_train_scaled, y_train)

    # 进行预测
    y_pred = ridge_best.predict(X_test_scaled)

    # 保存模型
    joblib.dump(ridge_best, "my_ridge_line.pkl")
    # 加载模型
    # estimator = joblib.load("my_ridge_line.pkl")

    # 5）得出模型
    print("梯度下降-权重系数为：\n", ridge_best.coef_)
    print("梯度下降-偏置为：\n", ridge_best.intercept_)

    # 6）模型评估
    print("预测房价：\n", y_pred)

    # 评估模型
    mse = mean_squared_error(y_test, y_pred)
    print(f'Mean Squared Error with best alpha: {mse}')

    # 注意：这里使用的是负均方误差作为评分指标，因为GridSearchCV默认寻找最大值，而均方误差越小越好，所以取负值。

    # 4. 可视化预测结果
    plt.scatter(y_test, y_pred, alpha=0.5)  # 绘制实际值与预测值的散点图
    plt.xlabel('Actual Values')
    plt.ylabel('Predicted Values')
    plt.title('Ridge Regression Prediction')

    # 绘制理想情况的对角线
    lims = [
        np.min([y_test.min(), y_pred.min()]),  # x轴最小值
        np.max([y_test.max(), y_pred.max()]),  # x轴最大值
    ]
    plt.plot(lims, lims, 'k--', alpha=0.75, zorder=0)
    plt.xlim(lims)
    plt.ylim(lims)

    # 显示图形
    plt.grid(True)
    plt.show()

    return None

if __name__ == "__main__":
    main()